package cn.com.api.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
//import org.apache.ibatis.annotations.Param;
//import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiT_member_idcard;
import cn.com.api.mapper.ApiT_member_idcardMapper;
import cn.com.api.mapper.ApiTmemberMapper;

//import cn.com.api.entity.ApiT_member_idcard;
//
//import cn.com.api.mapper.ApiT_member_idcardMapper;

import cn.com.api.service.ApiT_member_idcardService;
import cn.com.api.service.impl.base.T_member_idcardServiceImplBase;
import cn.com.juhe.JuheConfig;
import cn.com.juhe.JuheUtils;
import cn.com.util.DateUtil;
import cn.com.util.Utils;
import net.sf.json.JSONObject;

/**
 * 
 **/
@Service
public class ApiT_member_idcardServiceImpl extends T_member_idcardServiceImplBase implements ApiT_member_idcardService {

	@Autowired
	private ApiT_member_idcardMapper apiT_member_idcardMapper;
	@Autowired
	private ApiTmemberMapper apiTmemberMapper;

	public List<ApiT_member_idcard> findT_member_idcardByMap(Map map) {
		return apiT_member_idcardMapper.findT_member_idcardByMap(map);
	}

	@Override
	public List<Map<String, Object>> findMemberinfosByMap(Map map) {
		return apiT_member_idcardMapper.findMemberinfosByMap(map);
	}

	public Map<String, Object> identityAttestation(ApiT_member_idcard apiT_member_idcard) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		Boolean flag = true;
		if (apiT_member_idcard.getId_type() == 1) {// 身份证认证
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("key", JuheConfig.idCardkey);
			data.put("idcard", apiT_member_idcard.getId_no());
			data.put("realname", apiT_member_idcard.getName());
			String result = JuheUtils.net(JuheConfig.idCardUrl, data, "GET");
			JSONObject json = JSONObject.fromObject(result);
			Integer error_code = json.getInt("error_code");
			if (error_code == 0) {
				Integer res = json.getJSONObject("result").getInt("res");
				if (res != 1) {
					flag = false;
					// String message =
					// json.getJSONObject("result").getString("message");
					// map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
					// map.put(Utils.MESSAGE, message);
				}
			} else {
				flag = false;
				// String reason = json.getString("reason");
				// map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				// map.put(Utils.MESSAGE, reason);
			}
		}
		if (flag) {
			apiT_member_idcard.setApply_time(DateUtil.nowTimeToString());
			apiT_member_idcard.setStatus(0);
			map.put("user_id", apiT_member_idcard.getUser_id());
			List<ApiT_member_idcard> list = apiT_member_idcardMapper.findT_member_idcardByMap(map);
			map.clear();
			if (list != null && list.size() > 0) {// 修改
				apiT_member_idcard.setId(list.get(0).getId());
				apiT_member_idcardMapper.updateT_member_idcard(apiT_member_idcard);
			} else {
				apiT_member_idcardMapper.insertT_member_idcard(apiT_member_idcard);
			}
			map.put("cid", apiT_member_idcard.getUser_id());
			map.put("verify_idcard", 2 + "");
			apiTmemberMapper.modifyApiTmemberByCuserid(map);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "提交成功");
		} else {
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "身份信息不匹配");
		}
		return map;
	}

}
